package compiler;

import java.io.File;
import java.io.FileNotFoundException;

import basic.Er2svgsql;

/**
 * The Class MainParseAllFileInDir.
 */
public class MainParseAllFileInDir {

	/**
	 * The main method.
	 * 
	 * @param args
	 *            the arguments
	 * @throws ParseException
	 *             the parse exception
	 * @throws FileNotFoundException
	 *             the file not found exception
	 */
	public static void main(String args[]) throws ParseException,
	FileNotFoundException {
		File dir = new File("test");

		String[] children = dir.list();
		if (children == null) {
			// Either dir does not exist or is not a directory
		} else {
			for (int i = 0; i < children.length; i++) {

				if (extension(children[i]).compareTo("er") == 0) {
					// Get filename of file or directory
					String str = dir.getName() + "/" + filename(children[i]);
					System.out.println("\nFile: "+str);
					Er2svgsql parser = new Er2svgsql(str + ".er");
					parser.RunError();
					parser.writeErrorsToFile(str + ".log");
					parser.writeSQLToFile(str + ".sql");
					parser.writeSVGToFile(str + ".svg");
					parser.writeSVGToFileJpg(str + ".svg", str + ".jpg");
					parser.writeSVGToFilePdf(str + ".svg", str + ".pdf");
				}
			}
		}

	}

	/**
	 * Filename.
	 * 
	 * @param fullPath
	 *            the full path
	 * @return the string
	 */
	public static String filename(String fullPath) { // gets filename without
		// extension
		int dot = fullPath.lastIndexOf(".");
		int sep = fullPath.lastIndexOf("/");
		return fullPath.substring(sep + 1, dot);
	}

	/**
	 * Extension.
	 * 
	 * @param fullPath
	 *            the full path
	 * @return the string
	 */
	public static String extension(String fullPath) {
		int dot = fullPath.lastIndexOf(".");
		return fullPath.substring(dot + 1);
	}
}